﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace Caculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int clear = 1; decimal result, mresult = 0; string op;
        decimal eval(string op)
        {
            clear = 1;
            try
            {
                switch (op)
                {
                    case "+": result = result + Convert.ToDecimal(label1.Text); break;
                    case "-": result = result - Convert.ToDecimal(label1.Text); break;
                    case "/": result = result / Convert.ToDecimal(label1.Text); break;
                    case "*": result = result * Convert.ToDecimal(label1.Text); break;
                    case "Mod": result = result % Convert.ToDecimal(label1.Text); break;
                    case "^":result=Convert.ToDecimal(Math.Pow((Convert.ToDouble(result)), (Convert.ToDouble(label1.Text)))); break;
                    case "sqrty": result = Convert.ToDecimal(Math.Pow((Convert.ToDouble(result)), 1/ (Convert.ToDouble(label1.Text)))); break;
                    case "ln": result = Convert.ToDecimal(Math.Log((Convert.ToDouble(result)), (Convert.ToDouble(label1.Text)))); break;
                    case "log": result = Convert.ToDecimal(Math.Log10(((Convert.ToDouble(label1.Text))))); break;
                    default: result = Convert.ToDecimal(label1.Text); break;
                    

                }
            }
            catch (System.OverflowException) { label2.Text = ""; label2.Text = "Tran bo nho"; clear = 2;}
            catch (System.DivideByZeroException) { label2.Text = ""; label2.Text = "Khong the chia so 0"; clear = 2; }
            return result;
        }
        int Clear(int cl)
        {
            switch (cl)
            {
                case 1:
                    {
                        label1.Text = "";
                    } break;
                case 2:
                    {
                        label1.Text = ""; label2.Text = ""; op = "";
                    } break;
                case 3:
                    {
                        label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('r'));
                    } break;
                case 4:
                    {
                        label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('s'));
                    } break;
                case 5:
                    {
                        label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.LastIndexOf(' ') + 1);
                    } break;
            }
            return 0;
        }
        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "1";
            
        }
        private void buttonxoa_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length > 1)
            {
                label1.Text = label2.Text.Remove(label1.Text.Length - 1);
            }
            else if (label1.Text.Length <= 1)
            {
                Clear(1);
                clear = 1;
                label1.Text = "0";
            }

        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "2";
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "3";
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "5";
        }
        private void button6_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "6";
        }
        private void button7_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "7";
        }
        private void button8_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "8";
        }
        private void button9_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "9";
        }
        private void button0_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 28)
                label1.Text = label1.Text + "0";
           
        }

     

        private void buttonSUB_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " - ";
            else label2.Text = label2.Text + label1.Text + " - ";
            label1.Text = eval(op).ToString();
            op = "-";
        }

        private void buttonadd_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " + ";
            else label2.Text = label2.Text + label1.Text + " + ";
            label1.Text = eval(op).ToString();
            op = "+";
        }
        private void buttonMod_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " Mod ";
            else label2.Text = label2.Text + label1.Text + " Mod ";
            label1.Text = eval(op).ToString();
            op = "Mod";
        }
        private void buttonxmuy_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " ^ ";
            else label2.Text = label2.Text + label1.Text + " ^ ";
            label1.Text = eval(op).ToString();
            op = "^";
        }
        private void buttonln_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " ln ";
            else label2.Text = label2.Text + label1.Text + " ln ";
            label1.Text = eval(op).ToString();
            op = "ln";
        }
        private void buttonsqrty_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " sqrty ";
            else label2.Text = label2.Text + label1.Text + " sqrty ";
            label1.Text = eval(op).ToString();
            op = "sqrty";
        }
        private void buttondiv_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " / ";
            else label2.Text = label2.Text + label1.Text + " / ";
            label1.Text = eval(op).ToString();
            op = "/";
        }
        private void buttonnhan_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " x ";
            else label2.Text = label2.Text + label1.Text + " x ";
            label1.Text = eval(op).ToString();
            op = "x";
        }
        private void button1x_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("reciproc"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('r'), "reciproc("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "reciproc(" + label1.Text + ")";
            label1.Text = (1 / Convert.ToDecimal(label1.Text)).ToString(); clear = 3; 
        }
        private void buttoncan_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("sqrt"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "sqrt("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "sqrt(" + label1.Text + ")";
            label1.Text = Math.Sqrt(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttonn_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("N!"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "N!("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "N!(" + label1.Text + ")";
            decimal n = Convert.ToDecimal(label1.Text);
            int s = 1;
            for (int i = 1; i <= n; i++)
            {
                s = s * i;
            }
            label1.Text = s.ToString();
            clear = 4;
        }
        private void buttoncan3_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("sqrt3"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "sqrt3("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "sqrt3(" + label1.Text + ")";
            double t = (1 / 3);
            label1.Text = Math.Pow(Convert.ToDouble(label1.Text),t).ToString(); clear = 4;
        }
       
        private void buttonbinhphuong_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("sqr"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "sqr("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "sqr(" + label1.Text + ")";
            label1.Text = Math.Pow(Convert.ToDouble(label1.Text),2).ToString(); clear = 4;
        }
        private void buttonmu3_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("sqr"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "sqr("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "sqr(" + label1.Text + ")";
            label1.Text = Math.Pow(Convert.ToDouble(label1.Text), 3).ToString(); clear = 4;
        }
        private void buttonlog_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("Log"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "log("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "log(" + label1.Text + ")";
            label1.Text = Math.Log10(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttonEXP_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("EXP"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "EXP("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "EXP(" + label1.Text + ")";
            label1.Text = Math.Exp(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttonsin_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("sin"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "Sin("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "Sin(" + label1.Text + ")";
            label1.Text = Math.Sin(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttonsinh_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("sinh"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "Sinh("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "Sinh(" + label1.Text + ")";
            label1.Text = Math.Sinh(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttoncos_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("cos"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "cos("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "cos(" + label1.Text + ")";
            label1.Text = Math.Cos(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
          
        private void buttoncosh_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("cosh"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "cosh("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "cosh(" + label1.Text + ")";
            label1.Text = Math.Cosh(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttontan_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("tan"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "tan("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "tan(" + label1.Text + ")";
            label1.Text = Math.Tan(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttontanh_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("tanh"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "tanh("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "tanh(" + label1.Text + ")";
            label1.Text = Math.Tanh(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttontPi_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("Pi"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "Pi("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "Pi(" + label1.Text + ")";
            double t = 3.14;
            label1.Text = t.ToString(); clear = 4;
        }
        private void button10x_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("10^"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "10^("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "10^(" + label1.Text + ")";
            label1.Text = Math.Pow(10,Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        private void buttonphantram_Click(object sender, EventArgs e)
        {
            label1.Text = (result * Convert.ToDecimal(label1.Text) / 100).ToString();
            label2.Text = label2.Text + label1.Text; clear = 5;
        }
        private void buttoncongtru_Click(object sender, EventArgs e)
        {
            if (label1.Text.Contains('-'))
            {
                label1.Text = label1.Text.Remove(label1.Text.IndexOf('-'), 1);
            }
            else label1.Text = "-" + label1.Text;
        }
        private void buttonbang_Click(object sender, EventArgs e)
        {
            if (clear != 3 && clear != 4 && clear != 5)
                label2.Text = label2.Text + label1.Text;
            label1.Text = eval(op).ToString(); clear = 2;
        }
        private void buttonC_Click(object sender, EventArgs e)
        {
            Clear(1); clear = 1; label1.Text = "0";
        }
        private void buttonCE_Click(object sender, EventArgs e)
        {
            Clear(2); clear = 1; label1.Text = "0"; result = 0;
        }
        private void buttonMS_Click(object sender, EventArgs e)
        {
            mresult = Convert.ToDecimal(label1.Text); clear = 1; label2.Text = "M";
        }
        private void buttonMr_Click(object sender, EventArgs e)
        {
            label1.Text = mresult.ToString(); clear = 1;
        }
         private void buttonMcong_Click(object sender, EventArgs e)
        {
            mresult = mresult + Convert.ToDecimal(label1.Text); clear = 1; label2.Text = "M";
        }
         private void buttonMtru_Click(object sender, EventArgs e)
         {
             mresult = mresult - Convert.ToDecimal(label1.Text); clear = 1; label2.Text = "M";
         }
        private void buttonMC_Click(object sender, EventArgs e)
         {
             mresult = 0; clear = 1; label2.Text = "";
         }
       
    }
}
